package com.le.tester.book.concurrentjavafight.l10_1_3;

import com.le.tester.webpage.lock.Point;

import java.awt.*;
import java.util.HashSet;
import java.util.Set;

/**
 * createTime：2023/1/3 9:39
 * description：出租车可能发生死锁
 */
public class Taxi {

    private Point location, destination;

    private final Dispatcher dispatcher;

    public Taxi(Dispatcher dispatcher) {
        this.dispatcher = dispatcher;
    }

    public synchronized Point getLocation() {
        return location;
    }

    public synchronized void setLocation(Point point) {
        this.location = location;
        if (location.equals(destination)) {
            dispatcher.notifyAvailable(this);
        }
    }
}

class Dispatcher {
    private final Set<Taxi> taxis;

    private final Set<Taxi> availableTaxis;

    public Dispatcher() {
        taxis = new HashSet<>();
        availableTaxis = new HashSet<>();
    }

    public synchronized void notifyAvailable(Taxi taxi) {
        availableTaxis.add(taxi);
    }

    public synchronized Image getImage() {
        return null;
    }


}
